﻿using Himp.Platform.Logins;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;

namespace Himp.Platform
{
    /// <summary>
    /// 
    /// </summary>
    public class LogBaseService : PlatformAppService
    {
        private IHttpClientFactory _httpClientFactory;
        private ILogger<LogBaseService> _logger;
        protected string _baseUrl = "http://112.33.111.160:8088";
        private string _logUrl = "/api/v2/auth/login";
        private string _logUser = "1010331798@qq.com";
        private string _logPwd = "1q2w3E*";

        public LogBaseService(IHttpClientFactory httpClientFactory, ILogger<LogBaseService> logger)
        {
            _httpClientFactory = httpClientFactory;
            _logger = logger;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected async Task<string> Login()
        {
            var clinet = _httpClientFactory.CreateClient();

            LogLoginReq logLogin = new LogLoginReq()
            {
                Email = _logUser,
                Password = _logPwd
            };

            var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(logLogin), Encoding.UTF8, "application/json");
            var client = _httpClientFactory.CreateClient(nameof(LogBaseService));
            var responseModel = await client.PostAsync(_baseUrl + _logUrl, content);

            if (responseModel.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var logLoginRes = await responseModel.Content.ReadFromJsonAsync<LogLoginRes>();

                return logLoginRes.Token;
            }
            else
            {
                return string.Empty;
            }
        }
    }
}
